/**
 * 
 */
package com.insford.crawling.fifaonline3;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.BasicClientConnectionManager;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author insford
 *
 */
public class TeamList {
	
	private static String uri = "http://fifaonline.pmang.com/datacenter/proc.nwz?order=select_club&league_id=";
	private static String delemeter = "|";
	private String file = null;
	private String output = null;
	
	public String getOutput() {
		return output;
	}

	public void setOutput(String output) {
		this.output = output;
	}

	PrintWriter w = null;
	
	ArrayList<League> leagueList = new ArrayList<TeamList.League>();
	BasicClientConnectionManager conman = new  BasicClientConnectionManager();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TeamList t = new TeamList();
		t.setFile("src/data/league.txt");
		t.setOutput("src/data/team.txt");
		t.start();
	}
	
	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}
	
	public void start() {
		this.init();
		this.run();
		this.destroy();
	}

	public void init() {
		File f = new File(file);
		BufferedReader br = null;
		
		try {
			br = new BufferedReader(new FileReader(f));	
			
			String str = null;
			
			//첫번째라인 스킵
			br.readLine();
			while ((str = br.readLine()) != null) {
				String[] strAry = str.split("\\|");
				
				League l = new League();
				l.id = strAry[0];
				l.name = strAry[1];
				leagueList.add(l);
			}
			
			w = new PrintWriter(output);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void destroy() {
		
		if (w != null) {
			w.flush();
			w.close();
		}
		
		conman.shutdown();
	}
	
	
	public void run() {
		for (League l : leagueList) {
			request(l);
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void request(League l) {
		
		HttpClient httpClient = null;
		HttpEntity entity = null;
		
		try {
			httpClient = new DefaultHttpClient(conman);
			HttpGet get = new HttpGet(uri + l.id);
			
			HttpResponse resp = httpClient.execute(get);
			
			
			if (resp.getStatusLine().getStatusCode() == 200) {
				entity = resp.getEntity();
				String str = EntityUtils.toString(entity);
				
				
				ObjectMapper mapper = new ObjectMapper();
				@SuppressWarnings("unchecked")
				HashMap<String, String> teamMap = mapper.readValue(str, HashMap.class);
				
				for (String key : teamMap.keySet()) {
					String value = teamMap.get(key);
					w.println(l.id + delemeter + l.name + delemeter + key + delemeter + value);
				}
				
			}
			
			
		} catch (Exception e) {
			
		} finally {
			if (entity != null) {
				try {
					EntityUtils.consume(entity);
				} catch (IOException e) {}	
			}
			
		}
		
		
		
	}

	class League {
		String id = null;
		String name = null;
	}
}
